package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheim.client.pojo.Address;
import com.itheim.client.vo.AddressVo;
import com.itheima.common.R;
import com.itheima.service.AddressService;
import com.itheima.util.RequestParesUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.catalina.valves.ErrorReportValve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author longbosheng
 * @Date 2022/6/21 9:13
 * @PackageName:com.itheima.controller
 * @ClassName: AddresssController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
@Log4j2
@RequestMapping("/addressBook")
public class AddresssController {
    @Autowired
    private AddressService addressService;

    /**
     * 新增地址
     *
     * @param address
     * @param request
     * @return
     */
    @PostMapping
    public R AddAddress(@RequestBody Address address, HttpServletRequest request) {
        String id = RequestParesUtil.getRequestId(request);
        address.setUserId(id);
        address.setCreateUser(id);
        address.setUpdateUser(id);
        boolean save = addressService.save(address);
        if (save) {
            Address result = addressService.getById(address.getId());
            return R.success(result);
        }
        return R.error("添加功能异常");
    }

    /**
     * 删除地址
     *
     * @param
     * @return
     */
    @DeleteMapping
    public R DelAddress(@RequestParam List<String> ids) {
        boolean flag = addressService.removeByIds(ids);
        if (flag) {
            return R.success("删除地址成功");
        }
        return R.error("删除地址异常");
    }

    /**
     * 按用户id查询地址列表
     *
     * @param request
     * @return
     */
    @GetMapping("/list")
    public R AddressList(HttpServletRequest request) {
        String id = RequestParesUtil.getRequestId(request);
        List<AddressVo> addressVo = addressService.AddressList(id);
        return R.success(addressVo);
    }

    /**
     * 按照地址id查询地址信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R AddressInfo(@PathVariable String id) {
        AddressVo addressVo = addressService.AddressInfo(id);
        return R.success(addressVo);
    }

    /**
     * 按照地址id更新地址
     *
     * @param address
     * @return
     */
    @PutMapping
    public R updateAddress(@RequestBody Address address, HttpServletRequest request) {
        String id = RequestParesUtil.getRequestId(request);
        address.setUpdateUser(id);
        int flag = addressService.updateAddress(address);
        if (flag > 0) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    /**
     * 获取默认地址
     *
     * @return
     */
    @GetMapping("/default")
    public R getDefaultAddress(HttpServletRequest request) {
        String id = RequestParesUtil.getRequestId(request);
        AddressVo defaultAddress = addressService.getDefaultAddress(id);
        return R.success(defaultAddress);
    }

    /**
     * 设置为默认地址
     *
     * @return
     */
    @PutMapping("/default")
    public R setDefaultAddress(@RequestBody Address address,HttpServletRequest request) {
        String id = RequestParesUtil.getRequestId(request);
        Address oldaddress = addressService.getDefault(id);
        int flag1 =addressService.updateDefaultAddress(oldaddress);
        if (flag1>0){
            log.info("旧默认地址更新成功");
            int flag = addressService.setDefaultAddress(address);
            if (flag > 0) {
                return R.success("修改默认地址成功");
            }
            return R.error("修改默认地址失败");
        }else {
            return R.error("旧地址更新失败");
        }
    }
}